import event from '../util/NotifierManager';
import { INDEX, EVENT_SWITCH_2D_VIEW, EVENT_SWITCH_3D_VIEW } from '../util/constant';

import { configuration } from '../config/webapi';
import * as OpcUa from '../thirdparty/opcua-webapi'
import WebClient from '../util/webclient';
import Index2dHeat from './2d/index_heat';
import Index2dFurnace from './2d/index_furnace';
import Index2dMeter from './2d/index_meter';
import Index2dBlastFurnace from './2d/index_blastfurnace';
import Index3dBlastFurnace from './3d/index_blastfurnace'

export default class Main {
    constructor() {
        /** HT 2D 图纸 */
        this.g2d = new ht.graph.GraphView();
        /** HT 3D 场景 */
        this.g3d = new ht.graph3d.Graph3dView();

        /** 封装的用于对接Server端的接口 */
        this.uaClient = new WebClient(configuration);
        this.OpcUa = OpcUa;

        /** 初始化三个页面 */
        this.index2dHeat = new Index2dHeat(this.g2d, this);
        this.index2dFurnace = new Index2dFurnace(this.g2d, this);
        this.index2dMeter = new Index2dMeter(this.g2d, this);
        this.index2dBlastFurnace = new Index2dBlastFurnace(this.g2d, this);
        
        this.index3dBlastFurnace = new Index3dBlastFurnace(this.g3d, this);

        this.initView();

        /** 事件管理器 */
        this.event = event;
    }

    /**
     * 初始化2D图纸和3D场景
     *
     * @memberof Main
     */
    initView() {
        const g2d = this.g2d;
        const g3d = this.g3d;

        // 选中边框为0
        g2d.getSelectWidth = () => 0;
        // 禁止缩放
        g2d.setZoomable(false);
        // 禁止平移
        g2d.setPannable(false);
        // 禁止框选
        g2d.setRectSelectable(false);
        // 隐藏滚动条
        g2d.setScrollBarVisible(false);
        // 禁止图元移动
        g2d.setMovableFunc(() => false);

        const handler = e => {
            if (g2d.getDataAt(e)) {
                e.stopPropagation();
            }
        };
        g2d.getView().addEventListener('mousedown', handler);
        g2d.getView().addEventListener('touchstart', handler);
        g2d.getView().addEventListener('mousewheel', handler);

        g2d.addToDOM(g3d.getView());

        g3d.addToDOM();
    }

    /**
     * 用于切换页面
     *
     * @param {*} [key=INDEX]
     * @memberof Main
     */
    switch(key = INDEX) {
        this.switch2d(key);
        this.switch3d(key);
    }
    switch2d(key = INDEX) {
        event.fire(EVENT_SWITCH_2D_VIEW, key);
    }
    switch3d(key = INDEX) {
        event.fire(EVENT_SWITCH_3D_VIEW, key);
    }
}